// components/product_activity/product_activity.js
const util = require('../../utils/util.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    concise:{
      type:Boolean
    },
    tranString: {
      type: String,
      value: ''
    },
    promotions: {
      type: Array
    },
    point:{
      type:Number,
      value:0
    },
    price:{
      type:Number,
      value:''
    },
    marketPrice:{
      type: Number,
      value: ''
    },
    vipPrice:{
      type: Number,
      value: ''
    }
  },

  observers: {
    'promotions,price,point,marketPrice,vipPrice': function() {
     // 在组件实例进入页面节点树时执行
     this.updatePromotion()

    },
  },
  lifetimes: {
    attached() {
      console.log(this.data.point)
      this.updatePromotion()
    },
    ready() {


    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    activity: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    updatePromotion() {
      var _this = this
      _this.data.activity = []

      if(!util.isNull(_this.data.vipPrice)){
        if(_this.data.vipPrice < _this.data.price){
          var vipPrice = {
            price: _this.data.vipPrice,
            type: '100',
            title: 'VIP'
          }
          _this.data.activity.push(vipPrice)
        }
      }

      _this.data.promotions.forEach(function(item){
        if (item.type == '10' || item.type=='8') {
          _this.data.activity.push(item)
        }
      })

      _this.data.activity.sort((a, b) => { return a.price - b.price })

      // console.log(_this.data.activity)

      _this.setData({
        activity: _this.data.activity,
      })
    }

  }
})
